/**
 * 
 */
package com.and.rsspect;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.List;

import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

/**
 * RSS写文件操作类
 * @author dai.rui.lin
 * 
 */
public final class AndroidRSSWriter extends AbsRSSWriter {

	public AndroidRSSWriter() {

		setWrap(true);
	}

	public void writeRSS(RSS rss, String encoding,String version, File file) throws Exception {
		if (file != null && rss != null) {
			file.createNewFile();
		} else {
			throw new NullPointerException(
					"AndroidRSSWriter line 29 NullPointerException");
		}
		FileOutputStream out = new FileOutputStream(file);
		writeRSS(rss, encoding,version, out);
	}

	public void writeRSS(RSS rss, String encoding,String version, OutputStream out)
			throws Exception {
		String xmlString = writeRSS(rss, encoding,version);
		if (out == null) {
			throw new NullPointerException("OutputStream NULL");
		}
		out.write(xmlString.getBytes());
		Thread.yield();
		out.flush();
		if (out != null) {
			out.close();
		}
	}

	@Override
	String writeRSS(RSS rss, String encoding,String version) throws Exception {
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlSerializer xml = factory.newSerializer();
		StringWriter stringWriter = new StringWriter();
		xml.setOutput(stringWriter);
		xml.startDocument(encoding, false);
		writeWrap(xml);
		xml.startTag(null, "rss");
		xml.attribute(null, "version", version);
		writeWrap(xml);
		writeChannel(xml, rss.getChannel());
		writeWrap(xml);
		xml.endTag(null, "rss");
		String result = stringWriter.toString();
		if (stringWriter != null) {
			stringWriter.close();
			stringWriter = null;
		}
		return result;
	}

	@Override
	void writeChannel(XmlSerializer writer, Channel channel) throws Exception {
		writer.startTag(null, "channel");
		// title
		writeTitle(writer, channel.getTitle());
		writeImage(writer, channel.getImage());
		writeDescription(writer, channel.getDescription());
		writeLink(writer, channel.getLink());

		writeCopyright(writer, channel.getCopyright());
		writePubDate(writer, channel.getPubDate());
		writeLastBuildDate(writer, channel.getLastBuildDate());
		writeManagerEditor(writer, channel.getManagingEditor());
		writeWebMaster(writer, channel.getWebMaster());
		writeLanguage(writer, channel.getLanguage());
		writeGenerator(writer, channel.getGenerator());
		writeDocs(writer, channel.getDocs());
		writeItems(writer, channel.getItems());

		writeWrap(writer);

		writer.endTag(null, "channel");

	}

	@Override
	void writeItems(XmlSerializer writer, List<Item> items) throws Exception {
		if (items != null && items.size() > 0) {

			for (Item item : items) {
				writeWrap(writer);
				writer.startTag(null, "item");
				writeTitle(writer, item.getTitle());
				writeLink(writer, item.getLink());
				writeAuthor(writer, item.getAuthor());
				writeCategories(writer, item.getCategories());
				writePubDate(writer, item.getPubDate());
				writeComments(writer, item.getComments());
				writeDescription(writer, item.getDescription());
				writeWrap(writer);
				writer.endTag(null, "item");
			}

		}
	}

	@Override
	void writeAuthor(XmlSerializer writer, Author author) throws Exception {
		if (author != null) {
			writeWrap(writer);
			writer.startTag(null, "author");
			writer.text(author.getAuthor());
			writer.endTag(null, "author");

		}
	}

	@Override
	void writeCategories(XmlSerializer writer, List<Category> categories)
			throws Exception {

	}

	@Override
	void writeCloud(XmlSerializer writer, Cloud cloud) throws Exception {
		if (cloud != null) {
			writeWrap(writer);
			writer.startTag(null, "cloud");
			// <cloud domain="rpc.sys.com" port="80" path="/RPC2"
			// registerProcedure="myCloud.rssPleaseNotify" protocol="xml-rpc" />
			Attribute domain = cloud.getDomain();
			Attribute port = cloud.getPort();
			Attribute registre = cloud.getRegisterProcedure();
			Attribute protocol = cloud.getProtocol();
			if (domain != null) {
				writer.attribute(null, "domain", domain.getValue());
			}
			if(port!=null){
				writer.attribute(null, "port", port.getValue());
			}
			if(registre!=null){
				writer.attribute(null, "registerProcedure", registre.getValue());
			}
			if(protocol!=null){
				writer.attribute(null, "protocol", protocol.getValue());
			}
			writeWrap(writer);
			writer.endTag(null, "cloud");
		}
	}

	@Override
	void writeComments(XmlSerializer writer, Comments comments)
			throws Exception {
		if (comments != null) {
			writeWrap(writer);
			writer.startTag(null, "comments");
			if (comments.getComments() != null) {
				writer.text(comments.getComments());
			}
			writer.endTag(null, "comments");
		}

	}

	@Override
	void writeCopyright(XmlSerializer writer, Copyright copyright)
			throws Exception {
		if (copyright != null) {
			writeWrap(writer);
			writer.startTag(null, "copyright");
			writer.text(copyright.getCopyright());
			writer.endTag(null, "copyright");
		}

	}

	@Override
	void writeDescription(XmlSerializer writer, Description decription)
			throws Exception {
		if (decription != null) {
			writeWrap(writer);
			writer.startTag(null, "description");
			writer.text(decription.getDescription());
			writer.endTag(null, "description");
		}

	}

	@Override
	void writeDocs(XmlSerializer writer, Docs docs) throws Exception {
		if (docs != null) {
			writeWrap(writer);
			writer.startTag(null, "docs");
			if (docs.getDocs() != null) {
				writer.text(docs.getDocs());
			}
			writer.endTag(null, "docs");
		}
	}

	@Override
	void writeEnclosurce(XmlSerializer writer, Enclosure enclosure)
			throws Exception {
		if (enclosure != null) {
			writeWrap(writer);
			writer.startTag(null, "enclosure");

			// <enclosure url="http://live.curry.com/mp3/celebritySCms.mp3"
			// length="1069871" type="audio/mpeg"/>

			Attribute url = enclosure.getUrl();
			Attribute length = enclosure.getLength();
			Attribute type = enclosure.getType();
			if (url != null) {
				writer.attribute(null, url.getName(), url.getValue());
			}
			if (length != null) {
				writer.attribute(null, length.getName(), length.getValue());
			}
			if (type != null) {
				writer.attribute(null, type.getName(), type.getValue());
			}
			writer.endTag(null, "enclosure");
		}
	}

	@Override
	void writeExtensions(XmlSerializer writer, List<Extension> extensions)
			throws Exception {
		if (extensions != null && extensions.size() > 0) {
			List<Extension> lst = extensions;
			for (Extension ex : lst) {
				writer.startTag(null, ex.getElementName());
				List<Attribute> ats = ex.getAttributes();
				for (Attribute at : ats) {
					writer.attribute(null, at.getName(), at.getValue());
				}
				writer.text(ex.getContent());
				writer.endTag(null, ex.getElementName());
			}
		}
	}

	@Override
	void writeGenerator(XmlSerializer writer, Generator generator)
			throws Exception {
		if (generator != null) {
			writeWrap(writer);
			writer.startTag(null, "generator");
			writer.text(generator.getGenerator());
			writer.endTag(null, "generator");
		}
	}

	@Override
	void writeGUID(XmlSerializer writer, GUID guid) throws Exception {
		if (guid != null) {
			writeWrap(writer);
			if (guid.getGuid() != null) {
				writer.startTag(null, "guid");
				Attribute isPermaLink = guid.getIsPermaLink();
				if (isPermaLink != null) {
					writer.attribute(null, isPermaLink.getName(),
							isPermaLink.getValue());
				}
				writer.text(guid.getGuid());
				writer.endTag(null, "guid");
			}
		}

	}

	@Override
	void writeHeigth(XmlSerializer writer, Height height) throws Exception {
		if (height != null) {
			writeWrap(writer);
			if (height.getHeight() != null) {
				writer.startTag(null, "height");
				writer.text(height.getHeight());
				writer.endTag(null, "height");
			}
		}
	}

	@Override
	void writeImage(XmlSerializer writer, Image image) throws Exception {
		if (image != null) {
			writeWrap(writer);
			writer.startTag(null, "image");
			writeTitle(writer, image.getTitle());
			writeLink(writer, image.getLink());
			writeURL(writer, image.getUrl());
			writeWrap(writer);
			writer.endTag(null, "image");
		}
	}

	@Override
	void writeLanguage(XmlSerializer writer, Language language)
			throws Exception {
		if (language != null) {
			writeWrap(writer);
			writer.startTag(null, "language");
			writer.text(language.getLanguage());
			writer.endTag(null, "language");

		}

	}

	@Override
	void writeLastBuildDate(XmlSerializer writer, LastBuildDate lastBuildDate)
			throws Exception {
		if (lastBuildDate != null) {
			writeWrap(writer);
			writer.startTag(null, "lastBuildDate");
			writer.text(lastBuildDate.getText());
			writer.endTag(null, "lastBuildDate");
		}
	}

	@Override
	void writeLink(XmlSerializer writer, Link link) throws Exception {
		if (link != null) {
			writeWrap(writer);
			writer.startTag(null, "link");
			writer.text(link.getLink());
			writer.endTag(null, "link");
		}
	}

	@Override
	void writeManagerEditor(XmlSerializer writer, ManagingEditor managingEditor)
			throws Exception {
		if (managingEditor != null) {
			writeWrap(writer);
			writer.startTag(null, "managingEditor");
			if (managingEditor.getManagingEditor() != null) {
				writer.text(managingEditor.getManagingEditor());
			}
			writer.endTag(null, "managingEditor");
		}
	}

	@Override
	void writeName(XmlSerializer writer, Name name) throws Exception {
		if (name != null) {
			writeWrap(writer);
			writer.startTag(null, "name");
			writer.text(name.getName());
			writer.endTag(null, "name");
		}
	}

	@Override
	void writePubDate(XmlSerializer writer, PubDate pubDate) throws Exception {
		if (pubDate != null) {
			writeWrap(writer);
			writer.startTag(null, "pubDate");
			writer.text(pubDate.getText());
			writer.endTag(null, "pubDate");
		}

	}

	@Override
	void writeRating(XmlSerializer writer, Rating rating) throws Exception {

		if (rating != null) {
			writeWrap(writer);
			writer.startTag(null, "rating");
			writer.text(rating.getRating());
			writer.endTag(null, "rating");
		}
	}

	@Override
	void writeSkipDays(XmlSerializer writer, SkipDays skipDays)
			throws Exception {
		if (skipDays != null) {
			writeWrap(writer);
			writer.startTag(null, "skipDays");
			List<Day> days = skipDays.getSkipDays();
			if (days != null && days.size() > 0) {
				for (Day day : days) {
					writer.startTag(null, "day");
					writer.text(day.getDay());
					writer.endTag(null, "day");
				}
			}
			writer.endTag(null, "skipDays");
		}
	}

	@Override
	void writeSkipHours(XmlSerializer writer, SkipHours skipHours)
			throws Exception {
		if (skipHours != null) {
			writeWrap(writer);
			writer.startTag(null, "skipHours");
			List<Hour> hours = skipHours.getSkipHours();
			if (hours != null && hours.size() > 0) {
				for (Hour h : hours) {
					writer.startTag(null, "hour");
					writer.text(h.getHour());
					writer.endTag(null, "hour");
				}
			}
			writer.endTag(null, "skipHours");
		}
	}

	@Override
	void writeDays(XmlSerializer writer, List<Day> skipDays) throws Exception {

	}

	@Override
	void writeHours(XmlSerializer writer, List<Hour> skipHours)
			throws Exception {

	}

	@Override
	void writeSource(XmlSerializer writer, Source source) throws Exception {
		if (source != null) {
			writeWrap(writer);
			writer.startTag(null, "source");
			Attribute url = source.getUrl();
			if (url != null) {
				writer.attribute(null, url.getName(), url.getValue());
			}
			writer.text(source.getSource());
			writer.endTag(null, "source");
		}
	}

	@Override
	void writeTextInput(XmlSerializer writer, TextInput textInput)
			throws Exception {
		if (textInput != null) {
			writeWrap(writer);
			writer.startTag(null, "textInput");
			writeTitle(writer, textInput.getTitle());
			writeDescription(writer, textInput.getDescription());
			writeName(writer, textInput.getName());
			writeLink(writer, textInput.getLink());
			writer.endTag(null, "textInput");

		}
	}

	@Override
	void writeTitle(XmlSerializer writer, Title title) throws Exception {
		if (title != null) {
			writeWrap(writer);
			writer.startTag(null, "title");
			writer.text(title.getTitle());
			writer.endTag(null, "title");
		}
	}

	@Override
	void writeTTL(XmlSerializer writer, TTL ttl) throws Exception {
		if (ttl != null) {
			writeWrap(writer);
			if (ttl.getTtl() != null) {
				writer.startTag(null, "ttl");
				writer.text(ttl.getTtl());
				writer.endTag(null, "ttl");
			}
		}

	}

	@Override
	void writeURL(XmlSerializer writer, URL url) throws Exception {
		if (url != null) {
			writeWrap(writer);
			writer.startTag(null, "url");
			writer.text(url.getUrl());
			writer.endTag(null, "url");
		}

	}

	@Override
	void writeWebMaster(XmlSerializer writer, WebMaster webMaster)
			throws Exception {
		if (webMaster != null) {
			writeWrap(writer);
			writer.startTag(null, "webMaster");
			if (webMaster.getWebMaster() != null) {
				writer.text(webMaster.getWebMaster());
			}
			writer.endTag(null, "webMaster");
		}
	}

	@Override
	void writeWidth(XmlSerializer writer, Width width) throws Exception {
		if (width != null) {
			writeWrap(writer);
			if (width.getWidth() != null) {
				writer.startTag(null, "width");
				writer.text(width.getWidth());
				writer.endTag(null, "width");
			}
		}
	}

}
